package com.ogawa.chair7808.ui.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.ogawa.base.utils.CommonUtil;
import com.ogawa.chair7808.R;
import com.ogawa.chair7808.bean.Massage7808Armchair;
import com.ogawa.chair7808.manager.DataManager;
import com.ogawa.chair7808.manager.TimeBean;
import com.ogawa.chair7808.observer.TimeObserver;
import com.ogawa.chair7808.ui.iview.IAttitudeAdjustmentView7506;
import com.ogawa.chair7808.ui.presenter.AttitudeAdjustmentPressenter7506;
import com.ogawa.chair7808.utils.Activity7808Utils;
import com.example.reslib.utils.PreferenceWrapper;
import com.ogawa.base.Constant.CommmandNum;

import java.util.ArrayList;
import java.util.List;

import static com.ogawa.base.Constant.Constant.LIVE_EVENT_BUS_KEY_END_TIME;
import static com.ogawa.base.Constant.Constant.LIVE_EVENT_BUS_KEY_UPDATE_TIME;
import static com.ogawa.base.utils.StringUtils.numberToTime;


/**
 * 姿态调节
 */
public class AttitudeAdjustmentActivity7808 extends BaseMqtt7808Activity implements View.OnClickListener
//        , IAttitudeAdjustmentView7506
{
    private LinearLayout mLlReset;
    private LinearLayout mLlZero1;
    private LinearLayout mLlZero2;
    private ImageView mIvReset;
    private ImageView mIvZero1;
    private ImageView mIvZero2;
    private List<TextView> mIvAdjustViews = new ArrayList<>();
    private String mZero;
    //    private AttitudeAdjustmentPressenter7506 mPresenter;
    private TextView chairup;//升背
    private TextView chairdown; //降背
    private TextView chair_hand_up; //缩腿
    private TextView cahir_leg_down;//伸腿
    private TextView chair_foot_down; //降腿
    private TextView chair_foot_up;//升腿
    private ImageView mIvChair;

    @Override
    public int setLayoutId() {
        return R.layout.activity_attitude_adjustment_7808;
    }


    @Override
    public void initTitleBar() {
        super.initTitleBar();
        titleBar.setGreenBackBotton();
        titleBar.setTitle("姿态调节");
        setBackGround(R.mipmap.bg);
    }

    @Override
    public void initView(int layoutId) {
        super.initView(layoutId);
        initView();
        LiveEventBus.get(LIVE_EVENT_BUS_KEY_UPDATE_TIME, TimeBean.class).observe(this, new androidx.lifecycle.Observer<TimeBean>() {
            @Override
            public void onChanged(TimeBean timeBean) {
                long min = timeBean.getMin();
                long second = timeBean.getSecond();
            }
        });
        LiveEventBus.get(LIVE_EVENT_BUS_KEY_END_TIME, Integer.class).observe(this, new androidx.lifecycle.Observer<Integer>() {
            @Override
            public void onChanged(Integer timeBean) {
                if (CommonUtil.isActivityTop(AttitudeAdjustmentActivity7808.class, AttitudeAdjustmentActivity7808.this)) {
                    showFinishDialog(AttitudeAdjustmentActivity7808.this, "0", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            cancleFinishDialog();
                            Activity7808Utils.startHomeActivity(AttitudeAdjustmentActivity7808.this);
                            AttitudeAdjustmentActivity7808.this.finish();
                        }
                    });
                }
            }
        });
        updateView();
    }

    private void initView() {
        chairup = findViewById(R.id.chairup);
        chairdown = findViewById(R.id.chairdown);
        chair_hand_up = findViewById(R.id.chair_hand_up);
        cahir_leg_down = findViewById(R.id.cahir_leg_down);
        chair_foot_down = findViewById(R.id.chair_foot_down);
        chair_foot_up = findViewById(R.id.chair_foot_up);
        mIvChair = findViewById(R.id.chair);
        PreferenceWrapper mSp = new PreferenceWrapper();
        int mChoose = Integer.parseInt(mSp.getStringValue("app", "1"));
        mIvAdjustViews.add(chair_foot_up);
        mIvAdjustViews.add(chair_foot_down);
        mIvAdjustViews.add(cahir_leg_down);
        mIvAdjustViews.add(chair_hand_up);
        mIvAdjustViews.add(chairdown);
        mIvAdjustViews.add(chairup);
        mIvReset = findViewById(R.id.iv_reset);
        mIvZero1 = findViewById(R.id.iv_zero1);
        mIvZero2 = findViewById(R.id.iv_zero2);
        mLlReset = findViewById(R.id.ll_reset);
        mLlZero1 = findViewById(R.id.ll_zero1);
        mLlZero2 = findViewById(R.id.ll_zero2);
        mLlReset.setOnClickListener(this);
        mLlZero1.setOnClickListener(this);
        mLlZero2.setOnClickListener(this);
        chairup.setOnClickListener(this);
        chairdown.setOnClickListener(this);
        chair_hand_up.setOnClickListener(this);
        cahir_leg_down.setOnClickListener(this);
        chair_foot_down.setOnClickListener(this);
        chair_foot_up.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.chairup) {
            DataManager.getInst().sendCommond(CommmandNum.SHENBEI, "1");

        } else if (i == R.id.chairdown) {
            DataManager.getInst().sendCommond(CommmandNum.DAOBEI, "0");

        } else if (i == R.id.chair_hand_up) {
            DataManager.getInst().sendCommond(CommmandNum.SUOTUI, "0");

        } else if (i == R.id.chair_foot_down) {
            DataManager.getInst().sendCommond(CommmandNum.JIANGTUI, "0");

        } else if (i == R.id.chair_foot_up) {
            DataManager.getInst().sendCommond(CommmandNum.TAITUI, "1");

        } else if (i == R.id.cahir_leg_down) {
            DataManager.getInst().sendCommond(CommmandNum.SHENTUI, "1");

        } else if (i == R.id.ll_reset) {
            DataManager.getInst().sendCommond(CommmandNum.FUWEI, "0");

        } else if (i == R.id.ll_zero1) {
            DataManager.getInst().sendCommond(CommmandNum.LINZL1, "1");

        } else if (i == R.id.ll_zero2) {
            DataManager.getInst().sendCommond(CommmandNum.LINZL2, "2");

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        mPresenter.onDestory();
//        TimeObserver.getInst().removeObserver(this);
    }

    public void setZero(String zero) {
        if (zero.equals("0")) {
            mIvZero1.setSelected(false);
            mIvZero2.setSelected(false);
        } else if (zero.equals("1")) {
            mIvZero1.setSelected(true);
            mIvZero2.setSelected(false);
        } else if (zero.equals("2")) {
            mIvZero1.setSelected(false);
            mIvZero2.setSelected(true);
        }


    }

    void updateView() {
        mZero = DataManager.getInst().getDevice7808StateBean().getZeroGravity();
        setZero(mZero);
        mIvReset.setSelected(DataManager.getInst().getDevice7808StateBean().getResetSucccessFlag().equals("1"));
        String LegPosAdjust = DataManager.getInst().getDevice7808StateBean().getLegPosAdjust();
        if (LegPosAdjust.equals("1")) {
            chair_foot_up.setSelected(true);
            chair_foot_down.setSelected(false);
        } else if (LegPosAdjust.equals("2")) {
            chair_foot_up.setSelected(false);
            chair_foot_down.setSelected(true);
        } else {
            chair_foot_up.setSelected(false);
            chair_foot_down.setSelected(false);
        }
        String legPosAdjustX = DataManager.getInst().getDevice7808StateBean().getLegPosAdjustX();
        if (legPosAdjustX.equals("1")) {
            cahir_leg_down.setSelected(true);
            chair_hand_up.setSelected(false);
        } else if (legPosAdjustX.equals("2")) {
            cahir_leg_down.setSelected(false);
            chair_hand_up.setSelected(true);
        } else {
            cahir_leg_down.setSelected(false);
            chair_hand_up.setSelected(false);
        }
        String backPosAdjust = DataManager.getInst().getDevice7808StateBean().getBackPosAdjust();
        if (backPosAdjust.equals("1")) {
            chairdown.setSelected(true);
            chairup.setSelected(false);
        } else if (backPosAdjust.equals("2")) {
            chairdown.setSelected(false);
            chairup.setSelected(true);
        } else {
            chairdown.setSelected(false);
            chairup.setSelected(false);
        }
    }

//    @Override
//    public void isPause(boolean isPause) {
//        if (isPause) {
//            pauseTime();
//        } else {
//            reStarTime();
//        }
//    }

//    @Override
//    public void onupdataTime(String min, String sec) {
//        doUpDataTime(min, sec);
//    }


    @Override
    public void changeViewByMqttMessage() {
        updateView();
    }
}
